package com.itheima.mall.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.mall.mapper.ProductAttributeCategoryMapper;
import com.itheima.mall.mapper.ProductAttributeMapper;
import com.itheima.mall.mapper.ProductCategoryAttributeRelationMapper;
import com.itheima.mall.pojo.ProductAttribute;
import com.itheima.mall.pojo.ProductAttributeCategory;
import com.itheima.mall.pojo.ProductCategoryAttributeRelation;
import com.itheima.mall.service.PmsProductAttributeService;
import com.itheima.mall.vo.PageResult;
import com.itheima.mall.vo.ProductAttrInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Service
public class PmsProductAttributeServiceImpl implements PmsProductAttributeService {
    @Autowired
    private ProductAttributeMapper productAttributeMapper;
    @Autowired
    private ProductCategoryAttributeRelationMapper productCategoryAttributeRelationMapper;

    //根据商品分类的id获取商品属性及属性分类
    @Override
    public List<ProductCategoryAttributeRelation> selectByproductCategoryId(Integer productCategoryId) {
        LambdaQueryWrapper<ProductCategoryAttributeRelation> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(ProductCategoryAttributeRelation::getProductCategoryId , productCategoryId);

        List<ProductCategoryAttributeRelation> relationList = productCategoryAttributeRelationMapper.selectList(lambdaQueryWrapper);

        return relationList;
    }

    /**
     * 添加商品属性信息
     * @param productAttributeParam
     */
    @Override
    public void createProductAttribute(ProductAttribute productAttributeParam) {
        productAttributeMapper.insert(productAttributeParam);

    }

    //删除商品属性信息
    @Override
    public void deleteProductAttributeByIds(Integer[] ids) {
        ArrayList<Integer> list = new ArrayList<>(Arrays.asList(ids));
        productAttributeMapper.deleteBatchIds(list);
    }
    //修改商品属性信息
    @Override
    public void updateProductAttributeById(Integer id , ProductAttribute productAttribute) {
        LambdaQueryWrapper<ProductAttribute> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ProductAttribute::getId , id);

        productAttributeMapper.update(productAttribute , queryWrapper);
    }
    //通过id查找商品属性信息
    @Override
    public void selectProductAttributeById(Integer id) {
        productAttributeMapper.selectById(id);
    }
    //根据分类查询属性列表或参数列表
    @Override
    public PageResult selectPageByCidAndType(Integer cid, Long type, Integer pageNum, Integer pageSize) {
        //分页构造器
        Page<ProductAttribute> page = new Page<>(pageNum, pageSize);
        //根据分类类型查询
        LambdaQueryWrapper<ProductAttribute> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ProductAttribute::getProductAttributeCategoryId , cid);
        queryWrapper.eq(ProductAttribute::getType , type);

        Page<ProductAttribute> pageInfo = productAttributeMapper.selectPage(page, queryWrapper);
        return new PageResult(pageNum , pageSize , (int) pageInfo.getTotal(), pageInfo.getRecords());
    }


}
